﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class MessageBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IMessageCommandDA MessageCommandDA = DataAccess.CreateMessageCommandDA();
        private PubParasInfo pubParasInfo;

        public MessageBLL()
        {
            this.pubParasInfo = null;
        }

        public MessageBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public MessageBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteMessageCommand(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<MessageCommandInfo> messageCommandInfos = new List<MessageCommandInfo>();
                MessageCommandInfo messageCommandInfo = null;
                foreach (string id in IDS)
                {
                    messageCommandInfo = MessageCommandDA.GetMessageCommandByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    messageCommandInfos.Add(messageCommandInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (MessageCommandDA.DeleteMessageCommand(dataBase, tran, IDS, this.dataBaseID, this.pubParasInfo) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<MessageCommandInfo>(dataBase, tran, messageCommandInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CUSTOMER", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllMessageCommandInfo()
        {
            return MessageCommandDA.GetAllMessageCommand(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetMessageCommandInfoByID(string ID)
        {
            return MessageCommandDA.GetMessageCommandByID(ID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetMessageCommandInfoByQueryEntity(MessageCommandQueryEntity messageCommandQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(messageCommandQuery.WhID))
            {
                sqlWhere.Add(" WH_ID = '" + messageCommandQuery.WhID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageType))
            {
                sqlWhere.Add(" MESSAGE_TYPE = '" + messageCommandQuery.MessageType + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageID))
            {
                sqlWhere.Add(" MESSAGE_ID = '" + messageCommandQuery.MessageID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageTitle))
            {
                sqlWhere.Add(" MESSAGE_TITLE = '" + messageCommandQuery.MessageTitle + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsSend))
            {
                sqlWhere.Add(" MESSAGE_IS_SEND = '" + messageCommandQuery.MessageIsSend + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsRead))
            {
                sqlWhere.Add(" MESSAGE_IS_READ = '" + messageCommandQuery.MessageIsRead + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE = '" + messageCommandQuery.IsActive + "'");
            }
            return MessageCommandDA.GetMessageCommandByQueryList(sqlWhere, messageCommandQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetMessageCommandInfoByQueryEntity(List<string> sqlWhere, MessageCommandQueryEntity messageCommandQuery)
        {
            if (!string.IsNullOrEmpty(messageCommandQuery.WhID))
            {
                sqlWhere.Add(" WH_ID = '" + messageCommandQuery.WhID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageType))
            {
                sqlWhere.Add(" MESSAGE_TYPE = '" + messageCommandQuery.MessageType + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageID))
            {
                sqlWhere.Add(" MESSAGE_ID = '" + messageCommandQuery.MessageID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageTitle))
            {
                sqlWhere.Add(" MESSAGE_TITLE = '" + messageCommandQuery.MessageTitle + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsSend))
            {
                sqlWhere.Add(" MESSAGE_IS_SEND = '" + messageCommandQuery.MessageIsSend + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsRead))
            {
                sqlWhere.Add(" MESSAGE_IS_READ = '" + messageCommandQuery.MessageIsRead + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE = '" + messageCommandQuery.IsActive + "'");
            }
            return MessageCommandDA.GetMessageCommandByQueryList(sqlWhere, messageCommandQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<MessageCommandInfo> GetMessageCommandInfosByQueryEntity(MessageCommandQueryEntity messageCommandQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(messageCommandQuery.WhID))
            {
                sqlWhere.Add(" WH_ID = '" + messageCommandQuery.WhID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageType))
            {
                sqlWhere.Add(" MESSAGE_TYPE = '" + messageCommandQuery.MessageType + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageID))
            {
                sqlWhere.Add(" MESSAGE_ID = '" + messageCommandQuery.MessageID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageTitle))
            {
                sqlWhere.Add(" MESSAGE_TITLE = '" + messageCommandQuery.MessageTitle + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsSend))
            {
                sqlWhere.Add(" MESSAGE_IS_SEND = '" + messageCommandQuery.MessageIsSend + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsRead))
            {
                sqlWhere.Add(" MESSAGE_IS_READ = '" + messageCommandQuery.MessageIsRead + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE = '" + messageCommandQuery.IsActive + "'");
            }
            return MessageCommandDA.GetMessageCommandsByQueryList(sqlWhere, messageCommandQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<MessageCommandInfo> GetMessageCommandInfosByQueryEntity(List<string> sqlWhere, MessageCommandQueryEntity messageCommandQuery)
        {
            if (!string.IsNullOrEmpty(messageCommandQuery.WhID))
            {
                sqlWhere.Add(" WH_ID = '" + messageCommandQuery.WhID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageType))
            {
                sqlWhere.Add(" MESSAGE_TYPE = '" + messageCommandQuery.MessageType + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageID))
            {
                sqlWhere.Add(" MESSAGE_ID = '" + messageCommandQuery.MessageID + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageTitle))
            {
                sqlWhere.Add(" MESSAGE_TITLE = '" + messageCommandQuery.MessageTitle + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsSend))
            {
                sqlWhere.Add(" MESSAGE_IS_SEND = '" + messageCommandQuery.MessageIsSend + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.MessageIsRead))
            {
                sqlWhere.Add(" MESSAGE_IS_READ = '" + messageCommandQuery.MessageIsRead + "'");
            }
            if (!string.IsNullOrEmpty(messageCommandQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE = '" + messageCommandQuery.IsActive + "'");
            }
            return MessageCommandDA.GetMessageCommandsByQueryList(sqlWhere, messageCommandQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertMessageCommand(MessageCommandInfo messageCommandInfo)
        {
            string MessageID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                messageCommandInfo.UpdatedBy = messageCommandInfo.CreatedBy;
                messageCommandInfo.CreatedDate = DateTime.Now;
                messageCommandInfo.UpdatedDate = DateTime.Now;
                if (MessageCommandDA.InsertMessageCommand(dataBase, tran, messageCommandInfo, this.dataBaseID, this.pubParasInfo) > 0)
                {
                    MessageID = messageCommandInfo.MessageID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return MessageID;
        }

        public bool InsertMessageCommands(List<MessageCommandInfo> messageCommandInfos)
        {
            bool result = false;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                foreach (MessageCommandInfo messageCommandInfo in messageCommandInfos)
                {
                    messageCommandInfo.UpdatedBy = messageCommandInfo.CreatedBy;
                    messageCommandInfo.CreatedDate = DateTime.Now;
                    messageCommandInfo.UpdatedDate = DateTime.Now;
                }
                if (MessageCommandDA.InsertMessageCommands(dataBase, tran, messageCommandInfos, this.dataBaseID, this.pubParasInfo) > 0)
                {
                    result = true;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public string UpdateMessageCommand(MessageCommandInfo messageCommandInfo)
        {
            string ID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                messageCommandInfo.UpdatedDate = DateTime.Now;
                if (MessageCommandDA.UpdateMessageCommand(dataBase, tran, messageCommandInfo, this.dataBaseID, this.pubParasInfo) > 0)
                {
                    ID = messageCommandInfo.RowID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return ID;
        }
    }
}

